<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/19
 * Time: 11:41
 */

namespace Resource\Model;
use Think\Model;

class ItemCateModel extends Model
{
    protected $trueTableName = 'wz_item_category';

    /**
     * get
     * @param $params
     * @param string $fields
     * @return mixed
     */
    public function getCateInfo($params, $fields = '') {
        if($fields) {
            $info = $this->where($params)->field($fields)->find();
        } else {
            $info = $this->where($params)->find();
        }
        return $info;
    }

    /**
     * add
     * @param $params
     * @return mixed
     */
    public function addCate($params) {
        $id = $this->add($params);
        return $id;
    }

    /**
     * update&&save
     * @param $params
     * @return bool
     */
    public function saveCate($params) {
        $result = $this->save($params);
        if($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * all
     * @param $params
     * @return mixed
     */
    public function getAllCateList($params) {
        $list = $this->where($params)->order('status DESC,id ASC')->select();
        return $list;
    }

    /**
     * @param $params
     * @return mixed
     */
    public function getCateCount($params) {
        return $this->where($params)->count();
    }

    /**
     * @param $params
     * @param int $page
     * @param int $page_size
     * @return array
     */
    public function getCateList($params, $page=1, $page_size=20) {
        $list = $this->where($params)->order('create_time DESC')->limit($page_size)->page($page)->select();
        return $list ? $list : [];
    }

    /**
     * @param $params
     * @return string
     */
    public function getCateName($params) {
        $info = $this->where($params)->field('name')->find();
        return $info?$info['name']:'';
    }
}